using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Management;
using System.Text;
using System.Windows.Forms;

namespace csharpsysinfo
{
    public partial class NetworkInfoForm : Form
    {
        string networkDeviceStatusString;
        public NetworkInfoForm()
        {
            InitializeComponent();
        }

        private string GetNetworkAdapterInfo()
        {
            string NetworkAdapterName, networkDeviceNameString;
            int genericCounterInteger;

            genericCounterInteger = 1;

            NetworkAdapterName = "Failed to retrieve info.";
            //testString = "Failed to retrieve info.";

            try
            {

                ManagementObjectSearcher managementobjectsearcher = new ManagementObjectSearcher
                 ("SELECT * FROM Win32_NetworkAdapter");

                ManagementObjectCollection managementobjectcollection = managementobjectsearcher.Get();

                foreach (ManagementObject mob in managementobjectcollection)
                {
                    if (mob.Properties["NetConnectionStatus"].Value != null)
                    {
                        if (genericCounterInteger == 1)
                        {
                            networkDeviceNameString = mob.Properties["ProductName"].Value.ToString();
                            networkDeviceNameTextBox.Text = networkDeviceNameString;
                            networkDeviceStatusString = mob.Properties["NetConnectionStatus"].Value.ToString();
                            networkDeviceStatusTextBox.Text = networkDeviceStatusString;
                            GetNetworkStatus();
                            networkDeviceStatusTransTextBox.Text = GetNetworkStatus();
                        }
                        
                        if (genericCounterInteger == 2)
                        {
                            networkDeviceNameString = mob.Properties["ProductName"].Value.ToString();
                            networkDeviceName2TextBox.Text = networkDeviceNameString;
                            networkDeviceStatusString = mob.Properties["NetConnectionStatus"].Value.ToString();
                            networkDeviceStatus2TextBox.Text = networkDeviceStatusString;
                            GetNetworkStatus();
                            networkDeviceStatus2TransTextBox.Text = GetNetworkStatus();
                        }

                        if (genericCounterInteger == 3)
                        {
                            networkDeviceNameString = mob.Properties["ProductName"].Value.ToString();
                            networkDeviceName3TextBox.Text = networkDeviceNameString;
                            networkDeviceStatusString = mob.Properties["NetConnectionStatus"].Value.ToString();
                            networkDeviceStatus3TextBox.Text = networkDeviceStatusString;
                            GetNetworkStatus();
                            networkDeviceStatus3TransTextBox.Text = GetNetworkStatus();
                        }

                        if (genericCounterInteger == 4)
                        {
                            networkDeviceNameString = mob.Properties["ProductName"].Value.ToString();
                            networkDeviceName4TextBox.Text = networkDeviceNameString;
                            networkDeviceStatusString = mob.Properties["NetConnectionStatus"].Value.ToString();
                            networkDeviceStatus4TextBox.Text = networkDeviceStatusString;
                            GetNetworkStatus();
                            networkDeviceStatus4TransTextBox.Text = GetNetworkStatus();
                        }

                        if (genericCounterInteger == 5)
                        {
                            networkDeviceNameString = mob.Properties["ProductName"].Value.ToString();
                            networkDeviceName5TextBox.Text = networkDeviceNameString;
                            networkDeviceStatusString = mob.Properties["NetConnectionStatus"].Value.ToString();
                            networkDeviceStatus5TextBox.Text = networkDeviceStatusString;
                            GetNetworkStatus();
                            networkDeviceStatus5TransTextBox.Text = GetNetworkStatus();
                        }

                        if (genericCounterInteger == 6)
                        {
                            networkDeviceNameString = mob.Properties["ProductName"].Value.ToString();
                            networkDeviceName6TextBox.Text = networkDeviceNameString;
                            networkDeviceStatusString = mob.Properties["NetConnectionStatus"].Value.ToString();
                            networkDeviceStatus6TextBox.Text = networkDeviceStatusString;
                            GetNetworkStatus();
                            networkDeviceStatus6TransTextBox.Text = GetNetworkStatus();
                        }

                        if (genericCounterInteger == 7)
                        {
                            networkDeviceNameString = mob.Properties["ProductName"].Value.ToString();
                            networkDeviceName7TextBox.Text = networkDeviceNameString;
                            networkDeviceStatusString = mob.Properties["NetConnectionStatus"].Value.ToString();
                            networkDeviceStatus7TextBox.Text = networkDeviceStatusString;
                            GetNetworkStatus();
                            networkDeviceStatus7TransTextBox.Text = GetNetworkStatus();
                        }

                        if (genericCounterInteger == 8)
                        {
                            networkDeviceNameString = mob.Properties["ProductName"].Value.ToString();
                            networkDeviceName8TextBox.Text = networkDeviceNameString;
                            networkDeviceStatusString = mob.Properties["NetConnectionStatus"].Value.ToString();
                            networkDeviceStatus8TextBox.Text = networkDeviceStatusString;
                            GetNetworkStatus();
                            networkDeviceStatus8TransTextBox.Text = GetNetworkStatus();
                        }

                        genericCounterInteger++;
                    }
                }
            }
            catch
            {
            }
            return NetworkAdapterName;
        }

        private string GetNetworkStatus()
        {
            string networkDeviceStatusTransString;
            decimal networkDeviceStatusDecimal;

            //networkDeviceStatusDecimal = 0;
            networkDeviceStatusTransString = "Not Available";

            networkDeviceStatusDecimal = decimal.Parse(networkDeviceStatusString);


            if (networkDeviceStatusDecimal == 0)
            {
                networkDeviceStatusTransString = "Disconnected";
            }

            if (networkDeviceStatusDecimal == 1)
            {
                networkDeviceStatusTransString = "Connecting";
            }

            if (networkDeviceStatusDecimal == 2)
            {
                networkDeviceStatusTransString = "Connected";
            }

            if (networkDeviceStatusDecimal == 3)
            {
                networkDeviceStatusTransString = "Disconnecting";
            }

            if (networkDeviceStatusDecimal == 4)
            {
                networkDeviceStatusTransString = "Hardware not present";
            }

            if (networkDeviceStatusDecimal == 5)
            {
                networkDeviceStatusTransString = "Hardware disabled";
            }

            if (networkDeviceStatusDecimal == 6)
            {
                networkDeviceStatusTransString = "Hardware malfunction";
            }

            if (networkDeviceStatusDecimal == 7)
            {
                networkDeviceStatusTransString = "Media disconnected";
            }

            if (networkDeviceStatusDecimal == 8)
            {
                networkDeviceStatusTransString = "Authenticating";
            }

            if (networkDeviceStatusDecimal == 9)
            {
                networkDeviceStatusTransString = "Authentication succeeded";
            }

            if (networkDeviceStatusDecimal == 10)
            {
                networkDeviceStatusTransString = "Authentication failed";
            }

            if (networkDeviceStatusDecimal == 11)
            {
                networkDeviceStatusTransString = "Invalid address";
            }

            if (networkDeviceStatusDecimal == 12)
            {
                networkDeviceStatusTransString = "Credentials required";
            }

            return networkDeviceStatusTransString;
        }

        private void NetworkInfoForm_Load(object sender, EventArgs e)
        {
            //get network adapter information
            GetNetworkAdapterInfo();
            //networkInfoTextBox.Text = GetNetworkAdapterInfo();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GetNetworkAdapterInfo();
        }
    }
}